<template>

  <div style="margin-top: 20px;margin-left: 20px">
    <el-upload
      class="avatar-uploader"
      :action="host+'/guard/upload?module=setting'"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload">
      <img v-if="imageUrl" :src="host+imageUrl" class="avatar">
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
    <el-tag>客服二维码</el-tag>
  </div>



</template>

<script>
import { myGet,myPost } from '@/utils/myUtils'

export default {
  name: 'setting',
  data() {
    return {
      host:process.env.VUE_APP_HOST,
      imageUrl: ''
    };
  },
  methods: {
    getLst(){
      myGet('/guard/setting/lst').then(res=>{
        const records = res.data.records

        for (let i = 0; i < records.length; i++) {
          if(records[i].key==='qr'){
            this.imageUrl = records[i].val
          }
        }

      })
    },

    handleAvatarSuccess(res, file) {
      if(res.status){
        this.imageUrl =res.data.records.filePath

        myPost('/guard/setting/add',{qr:this.imageUrl}).then(res=>{

        })

      }else{
        this.$message.error('上传失败!')
      }
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        //this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        //this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return true
      //return isJPG && isLt2M;
    }
  },
  mounted() {
    this.getLst()
  }
}
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
